package com.tem.system.controller;

import com.api.manage.pojo.system.SysRegionBean;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.tem.common.tool.result.ResultUtil;
import org.springframework.beans.BeanUtils;
import com.tem.system.entity.SysRegion;
import com.tem.system.service.ISysRegionService;
import com.tem.common.core.base.BaseController;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 区域 前端控制器
 * </p>
 * @author 鸿源塔基
 * @since 2023-06-09
 */
@RestController
@RequestMapping("/system/sysRegion")
public class SysRegionController extends BaseController{
    @Autowired
    private ISysRegionService sysRegionService;

    @ApiOperation(value = "区域列表")
    @RequestMapping("list")
    public ResultUtil<IPage<SysRegionBean>> list(SysRegionBean request){
        List<SysRegionBean> responseList=sysRegionService.getTree();
        Page page = new Page<>(1,responseList.size(),responseList.size());
        page.setRecords(responseList);
        return ResultUtil.success(page);
    }
    @ApiOperation(value = "区域列表下拉选择")
    @RequestMapping("listSelect")
    public ResultUtil<IPage<SysRegionBean>> listSelect(SysRegionBean request){
        if(request.getParentId()==null)request.setParentId(0l);
        QueryWrapper<SysRegion> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",request.getParentId());
        queryWrapper.orderByDesc("sort");
        List<SysRegion> list=sysRegionService.list(queryWrapper);
        List<SysRegionBean> responseList=sysRegionService.toBean(list);
        Page page = new Page<>(1,responseList.size(),responseList.size());
        page.setRecords(responseList);
        return ResultUtil.success(page);
    }
    @PostMapping("save")
    @ApiOperation(value = "新增区域")
    public ResultUtil save(@RequestBody SysRegion entity){
        //entity.setCreateTime(LocalDateTime.now());entity.setDeleted(0);
        if(entity.getSysRegionId()!=null){
            sysRegionService.updateById(entity);
        }else{
            entity.setDeleted(0);
            sysRegionService.save(entity);
        }
        return ResultUtil.success();
    }

    @GetMapping("getById/{id}")
    @ApiOperation(value = "id查询区域")
    public ResultUtil<SysRegionBean> getById(@PathVariable String id){
        SysRegion entity = sysRegionService.getById(id);
        if(entity!=null){
            SysRegionBean bean = new SysRegionBean();
            BeanUtils.copyProperties(entity,bean);
            return ResultUtil.success(bean);
        }
        return ResultUtil.error("没有找到对象");
    }

    @PutMapping("update")
    @ApiOperation(value = "修改区域")
    public ResultUtil update(@RequestBody SysRegion entity){
        sysRegionService.updateById(entity);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除区域")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable Long id){
        sysRegionService.removeById(id);
        return ResultUtil.success();
    }

    private static long deid=1;
    @ApiOperation(value = "区域数据初始化")
    @RequestMapping("initlist346457568")
    public String initlist53457() {
        deid=1;
        List<SysRegion> roots1 = getChi(1);
        for(SysRegion s:roots1){//第一层
            this.doUp(0l,"",s);
        }
        List<SysRegion> sysRegions2 = getChi(2);
        doFcSave(sysRegions2,roots1);

        List<SysRegion> sysRegions3 = getChi(3);//第三层
        doFcSave(sysRegions3,sysRegions2);

        List<SysRegion> sysRegions4 = getChi(4);//
        doFcSave(sysRegions4,sysRegions3);

        List<SysRegion> sysRegions5 = getChi(5);//
        doFcSave(sysRegions5,sysRegions4);

        List<SysRegion> sysRegions6 = getChi(6);//
        doFcSave(sysRegions6,sysRegions5);
        return "1";
    }

    private void doFcSave(List<SysRegion> nowList,List<SysRegion> parentList){
        if(nowList==null || nowList.size()<1)return;
        for(SysRegion s:nowList){//第二层
            for(SysRegion r:parentList){
                if(s.getParentCode().equals(r.getCode())){
                    this.doUp(r.getSysRegionId(),r.getRegionPath(),s);
                }
            }
        }
    }
    private List<SysRegion> getChi(Integer type){
        QueryWrapper<SysRegion> wrapper = new QueryWrapper<>();
        wrapper.eq("type",type);
        wrapper.orderByAsc("sort");
        return sysRegionService.list(wrapper);
    }
    private void doUp(long parentId,String parentPath,SysRegion up){
        up.setParentId(parentId);
        up.setSysRegionId(deid);
        up.setRegionPath(parentPath+deid+",");
        deid += 1l;
        QueryWrapper<SysRegion> upQw = new QueryWrapper<>();
        upQw.eq("code",up.getCode());
        sysRegionService.update(up,upQw);
    }
}